<?php
class PrivilegeController extends BaseController
{
	public function index()
	{
		$this->view('sysprivs');
	}

	public function view($name = null, $table = null)
	{
		$userName  = $this->registry->db->get('userManager')->getCurrentUser();
		$this->registry->template->setMenuKey('privilege');
		$man = $this->registry->db->get('privilegeManager');
		$tem = $this->registry->template;

		if ($name == 'sysprivs') {
			$tags['manuContentTitle'] = 'System Privilege Management';
			$content = $this->getSysPrivTbl($userName);
		}
		else if ($name == 'objprivs') {
			$tags['manuContentTitle'] = 'Object Privilege Management';
			$content = $this->getObjPrivTbl($userName);
		}
		else {
			if ($man->isSysPriv($name)) {
				$tags['manuContentTitle'] = 'View System Privilege: ' . $name;
				$atts = array('PRIVILEGE');
				$content = $this->getGeneralTable($name, $atts, 'privilege') . '</br>' .
				$this->getSysUserTbl($name);
			}
			else {
				$tags['manuContentTitle'] = 'View Object Privilege: ' . $name;
				$atts = array('PRIVILEGE');
				if ($table == null) {
					$content = $this->getGeneralTable($name, $atts, 'privilege') . '</br>' .
					$this->getTableNameTbl($name);
				}
				else {
					array_push($atts, 'TABLE_NAME');
					$content = $this->getGeneralTable($name, $atts, 'privilege', $table) . '</br>' .
					$this->getObjUserTbl($name, $table);
				}
			}
		}

		$tags['path'] = MANU_ROOT_PATH . 'privilege';

		$tags['what'] = 'privilege';
		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
						
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);


			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}

		$output = $this->registry->template->getPageContent('index', 'Oracle User Manager', $content, $tags);

		echo $output;
	}

	
	
	public function getSysPrivTbl($userName = null)
	{
		//TODO:	Need to modify

		// get system privileges
		$atts = array('PRIVILEGE');
		$rows = $this->registry->db->get('privilegeManager')->viewSysPriv($userName);
		$res = $this->getEntityTable($atts, $rows, 'privilege');

		return $res;
	}

	public function getObjPrivTbl($userName)
	{
		$privMan = $this->registry->db->get('privilegeManager');
		
		// get object privileges
		if ($this->registry->db->get('privilegeManager')->hasDBA($userName)) {
			$atts = array('PRIVILEGE');
		}
		else {
			$atts = array('PRIVILEGE', 'TABLE_NAME', 'GRANTABLE');
		}			
		
		//$user = $this->registry->db->get('userManager')->getCurrentUser();
		$rows = $privMan->getObjPrivsFrom($userName);
		if (!$privMan->hasDBA($userName)) {
			$tables = $privMan->getTableofUser($userName);
			foreach ($tables as $table) {
				$obj = $this->registry->db->getClassName('privilege');
				$obj->set('PRIVILEGE', 'SELECT');
				$obj->set('GRANTABLE', null);
				$obj->set('TABLE_NAME', $table->get('TABLE_NAME'));
				array_push($rows, $obj);
				
				$obj = $this->registry->db->getClassName('privilege');
				$obj->set('PRIVILEGE', 'INSERT');
				$obj->set('GRANTABLE', null);
				$obj->set('TABLE_NAME', $table->get('TABLE_NAME'));
				array_push($rows, $obj);
				
				$obj = $this->registry->db->getClassName('privilege');
				$obj->set('PRIVILEGE', 'DELETE');
				$obj->set('GRANTABLE', null);
				$obj->set('TABLE_NAME', $table->get('TABLE_NAME'));
				array_push($rows, $obj);
			}
		}
		
		$res = $this->getEntityTable($atts, $rows, 'privilege', /*grant*/ true);

		return $res;
	}

	/*
	 * @description:	Get users who is granted the system priv
	*/
	public function getSysUserTbl($priv)
	{
		$man = $this->registry->db->get('privilegeManager');
		$tem = $this->registry->template;

		$atts = array('GRANTEE', 'ADMIN_OPTION');
		$rows = $man->getGranteeOf($priv, 'dba_sys_privs');

		$res = '<div id="title">Users Granted</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	/*
	 * @description:	Get users who is granted the system priv
	*
	*/
	public function getObjUserTbl($priv, $table)
	{
		$man = $this->registry->db->get('privilegeManager');
		$tem = $this->registry->template;

		$atts = array('GRANTEE', 'GRANTABLE');
		$rows = $man->getGranteeOf($priv, 'dba_tab_privs', $table);

		$res = '<div id="title">Users Granted</div><hr>';
		$res .= $tem->getDetailTable($atts, $rows);

		return $res;
	}

	/*
	 * @description:	Get TABLE_NAME table that one privilege involve in
	*
	*/
	public function getTableNameTbl($priv)
	{
		$man = $this->registry->db->get('privilegeManager');
		$tem = $this->registry->template;

		$atts = array('TABLE_NAME');
		$rows = $man->getTableNameOf($priv);

		$res = '<div id="title">On Tables</div><hr>';

		// table's header
		$header = '<tr><th>TABLE_NAME<th></tr>';

		// table's body
		$body = '';
		foreach ($rows as $obj) {
			$tblName =  $obj->get('TABLE_NAME');
			$body .= '<tr><td><a href="{path}' . $priv . "/" . $tblName .'">' . $tblName . '</a></td></tr>';
		}

		$tags = array('manuTableHeader' => $header ,
					  'manuTableContent' => $body ,
					  'path' => MANU_ROOT_PATH . 'privilege/view/');

		$tbl = $tem->getModule('table', $tags);

		$res .= $tbl;

		return $res;
	}

	public function editGrant($priv = null, $table = null){
		$this->registry->template->setMenuKey('privilege');
		
		$method = $_SERVER['REQUEST_METHOD'];
		$userMan = $this->registry->db->get('userManager');
		$roleMan = $this->registry->db->get('roleManager');
		$privMan = $this->registry->db->get('privilegeManager');
		$tem = $this->registry->template;
		
		$user = $userMan->getCurrentUser();
		
		$tags['priv_val'] = $priv;
		$tags['userOrRole'] = "";
		if($method == 'POST') {			
			if (isset($_POST["table_name"])) {									
				$table = strip_tags($_POST["table_name"]);					
			}
			if ($table != null) {	
				$obj = 	$privMan->getOwnerOfTable($table);	
				if ($obj != null)
					$table = $obj->get('OWNER') . "." . $table;
			}
			if (isset($_POST["granttorole"])) {
				$granttorole = strip_tags($_POST["granttorole"]);
			}
			if(isset($_POST["granttouser"])) {
				$granttouser = strip_tags($_POST["granttouser"]);
			}
			$adminoption = null;
			if(isset($_POST["adoption"])) {
				$adminoption = strip_tags($_POST["adoption"]);
			}
			if(isset($_POST["revokeuser"])) {
				$revoke = strip_tags($_POST["revokeuser"]);
			}
			if ($table == null) {
				$privMan->editGrantSysPrivs($priv, $granttorole, $granttouser, $revoke, $adminoption );
				header("Location: ../view/sysprivs");
				exit();
			}
			else {
				$privMan->editGrantObjPrivs($priv, $table, $granttorole, $granttouser, $revoke, $adminoption );
				if ($privMan->hasDBA($user))
					header("Location: ../view/objprivs");
				else 
					header("Location: ../../view/objprivs");
				exit();
			}
			
		}
		else {			
			$type = 'syspriv';
			$tags['tables'] = '';
			if ($table != null || !$privMan->isSysPriv($priv)) {
				$type = 'objpriv';
				$tags['manuContentTitle'] = 'Grant/Revoke Object Privilege';
				
				if ($table != null) {
					$tags['tables'] = '<tr>
									   <th valign="top">ON TABLE</th>
									   <td><input type="text" name="table_name" value="' . $table . '" class="inp-form2" disabled="disabled"/></td>
									   <td>
											<div class="error-left"></div>
											<div class="error-inner"></div>
									   </td>
									   </tr>';
				}
				else {
					
					$tbls = $privMan->getTableNameList($priv);
					$tags['tables'] = '<tr>
									   <th valign="top">ON TABLE</th>
											<td><select name="table_name" class="styledselect_form_3" value="All">
												<option value=""></option>';
					foreach ($tbls as $tbl) {
						$tags['tables'] .= "<option value=" . $tbl->get('TABLE_NAME').">" . $tbl->get('TABLE_NAME')."</option>";
					}
					$tags['tables'] .= '</select></td>
										<td>
											<div class="error-left"></div>
											<div class="error-inner"></div>
										</td></tr>';
				}
			}
			else {
				$tags['manuContentTitle'] = 'Grant/Revoke System Privilege';
			}

			$res2 = $roleMan->getBasicInfo();
			$tags['userOrRole'] = "";
			foreach ($res2 as $re2) {
				$tags['userOrRole'] .= "<option value=".$re2->get('ROLE').">".$re2->get('ROLE')."</option>";
			}

			$res3 = $userMan->getBasicInfo();
			$tags['alluser'] = "";
			foreach ($res3 as $re3) {
				$tags['alluser'] .= "<option value=".$re3->get('USERNAME').">".$re3->get('USERNAME')."</option>";
			}

			if ($type == 'syspriv')
				$res4 = $privMan->getUserOfSysPriv($priv);
			else 
				$res4 = $privMan->getUserOfObjPriv($priv);			
			$tags['grantee'] = "";
			foreach ($res4 as $re4) {
				$tags['grantee'] .= "<option value=".$re4->get('GRANTEE').">".$re4->get('GRANTEE')."</option>";
			}
			$content = $tem->getModule('editGrantPrivs', $tags);
		}
			
		$tags['what'] = 'privilege';
		if (isset($_SESSION['message'])) {
			$tags['action'] = $_SESSION['message'];
			$tags['manuMessage'] = $tem->getModule('sucmess', $tags);


			unset($_SESSION['message']);
		}
		else {
			$tags['manuMessage'] = '';
		}
			
		$output = $tem->getPageContent('index', 'Oracle User Manager', $content, $tags);
			
		echo $output;
	}
}
?>